package class5;

import common.TreeNode;
import common.TreeNodeUtils;

public class Code12_PrintTree {
    public static void main(String[] args) {
        TreeNode tr = TreeNodeUtils.getCBT();
        printTree(tr);
    }

    public static void printTree(TreeNode node) {
        System.out.println("Binary Tree:");
        printInOrder(node, 0, "H", 17);
    }

    private static void printInOrder(TreeNode head, int high, String to, int len) {
        if (head == null) {
            return;
        }
        printInOrder(head.right, high + 1, "v", len);
        String val = to + head.val + to;
        int lenM = val.length();
        int lenL = (len - lenM) / 2;
        int lenR = len - lenM - lenL;
        val = getSpace(lenL) + val + getSpace(lenR);
        System.out.println(getSpace(high * len) + val);
        printInOrder(head.left, high + 1, "^", len);
    }

    public static String getSpace(int num) {
        String space = " ";
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < num; i++) {
            buf.append(space);
        }
        return buf.toString();
    }
}

